/*
 *  Copyright 2005 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.action.core.menuItem;

import com.blandware.atleap.webapp.action.core.BaseAction;
import com.blandware.atleap.webapp.form.core.SwitchMenuItemSetVisibilityForm;
import com.blandware.atleap.webapp.util.core.WebappConstants;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.commons.validator.GenericValidator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Map;


/**
 * <p>Process set of selected menu items
 * </p>
 * <p><a href="ProcessItemSetAction.java.html"></a><i>View source</i></p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.7 $ $Date: 2006/08/03 10:08:00 $
 * @struts.action path="/core/menuItem/processItemSet"
 * name="switchMenuItemSetVisibilityForm"
 * scope="request"
 * validate="false"
 * roles="core-menuItem-switchVisibility"
 * @struts.action-forward name="switchMenuItemVisibility"
 * path="/core/menuItem/switchVisibility.do"
 * redirect="false"
 * @struts.action-forward name="listMenuItems"
 * path="/core/menuItem/list.do"
 * redirect="false"
 */
public final class ProcessItemSetAction extends BaseAction {
	/**
	 * @param mapping  The ActionMapping used to select this instance
	 * @param form     The optional ActionForm bean for this request (if any)
	 * @param request  The HTTP request we are proceeding
	 * @param response The HTTP response we are creating
	 * @return an ActionForward instance describing where and how
	 *         control should be forwarded, or null if response
	 *         has already been completed
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
	                             HttpServletRequest request, HttpServletResponse response) throws Exception {

		SwitchMenuItemSetVisibilityForm processSetForm = (SwitchMenuItemSetVisibilityForm) form;
		ArrayList selectedItemIdsList = new ArrayList(processSetForm.getCheckedBoxes().keySet());
        Map positions = processSetForm.getPositions();

		if ( selectedItemIdsList.isEmpty() ) {
			return mapping.findForward("listMenuItems");
		}

        String ownerId = request.getParameter("ownerId");
        if (GenericValidator.isBlankOrNull(ownerId)) {
            return mapping.findForward("listMenuItems");
        }

        String mode = null;
        String multipleOperation = request.getParameter("multipleOperation");

		ActionForward forward = null;
        forward = mapping.findForward("switchMenuItemVisibility");
        if (!GenericValidator.isBlankOrNull(multipleOperation)) {
            if ("hideSelected".equalsIgnoreCase(multipleOperation)) {
                mode = "invisible";
            } else if ("showSelected".equalsIgnoreCase(multipleOperation)) {
                mode = "visible";
            } else if ("inheritVisibilityForSelected".equalsIgnoreCase(multipleOperation)) {
                mode = "heritable";
            } else {
                return mapping.findForward("listMenuItems");
            }
        } else {
            if ( request.getParameter("hideSelected") != null ) {
                mode = "invisible";
            } else if ( request.getParameter("showSelected") != null ) {
                mode = "visible";
            } else if ( request.getParameter("inheritVisibilityForSelected") != null ) {
                mode = "heritable";
            } else {
                return mapping.findForward("listMenuItems");
            }
        }

        request.setAttribute(WebappConstants.MENU_ITEM_SELECTED_IDS_KEY, selectedItemIdsList);
        request.setAttribute(WebappConstants.MENU_ITEM_VISIBILITY_MODE_FOR_SELECTED_KEY, mode);
        request.setAttribute(WebappConstants.MENU_ITEM_OWNER_ID_FOR_SELECTED_KEY, ownerId);
        request.setAttribute(WebappConstants.MENU_ITEM_POSITIONS_KEY, positions);

		return forward;
	}
}
